<script>
import autoWxUpdate from "@/js/autoWxUpdate.js"
import $http from "@/api"
export default {
  globalData: {
    aphone: '',
    isLogin: false,
    userData: {},
    token: '',
    oneClassId: 0,
    canjia_link: '',
    systemInfo: {},
    orderType: '',
  },
  onLaunch: function () {
    uni.hideTabBar();
    uni.removeStorageSync('editLocation')
    const self = this
    uni.getSystemInfo({
      success: function (data) {
        self.globalData.systemInfo = data
      }
    })
    // #ifdef APP-PLUS
    plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
      self.globalData.systemInfo.version = wgtinfo.version; //应用版本号
      self.globalData.systemInfo.code = wgtinfo.versionCode; //应用版本号
      console.log(wgtinfo);
      self.upVer(wgtinfo.versionCode)
    });
    // #endif
    // #ifdef MP-WEIXIN
    autoWxUpdate()
    // #endif
    const result = uni.getSystemInfoSync();
    let screenHeight = result.windowHeight;
    let safeAreaBottom = result.screenHeight - result.safeArea.bottom;
    let statusBarHeight = result.statusBarHeight
    uni.setStorageSync('statusBarHeight', statusBarHeight);
    uni.setStorageSync('screenHeight', screenHeight);
    uni.setStorageSync('safeAreaBottom', safeAreaBottom)
  },
  onShow: function () {
    if (uni.getStorageSync("token")) {
    };
  },
  onHide: function () {
    console.log('App Hide')
  },
  methods: {
    versionStringTONumber(version) {
      var version = version.split(".");
      var versionNumber = 0;
      for (var i = 0; i < version.length; i++) {
        versionNumber +=
          parseInt(version[i]) * Math.pow(10, version.length - i - 1);
      }
      return versionNumber;
    },
    updateVersion() {
      // plus.runtime.getProperty(),获取本机安装的版本，具体使用请参照如下链接
      // https://www.html5plus.org/doc/zh_cn/runtime.html#plus.runtime.WidgetInfo
      plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
        let currentVersion = widgetInfo.version.replace(/\./g, ""); //获取当前版本号
        $http.base.getVersion().then((res) => {
          let newVersion = this.versionStringTONumber(res.vs_see);
          let downloadUrl = res.url;
          if (res.status == 1) {
            if (currentVersion < newVersion) {
              uni.showModal({
                title: "已发现新版本",
                content: "确认更新？",
                success: function (res) {
                  if (res.confirm) {
                    uni.downloadFile({
                      url: downloadUrl,
                      success: (downloadResult) => {
                        plus.runtime.install(
                          downloadResult
                            .tempFilePath, {
                          force: true,
                        },
                          function () {
                            // 应用热重启，重新启动进入首页。
                            plus.runtime
                              .restart();
                            uni.showModal({
                              title: "安装成功！",
                              content: "已重新进入应用",
                              success: function (
                                res
                              ) {
                                if (res
                                  .confirm
                                ) {
                                  console
                                    .log(
                                      "用户点击确定"
                                    );
                                } else if (
                                  res
                                    .cancel
                                ) {
                                  console
                                    .log(
                                      "用户点击取消"
                                    );
                                }
                              },
                            });
                          },
                          function (e) {
                            uni.showToast({
                              title: "安装失败",
                              icon: "none",
                            });
                          }
                        );
                      },
                      fail: (err) => {
                        console.log(err);
                        uni.showToast({
                          title: "下载失败",
                          icon: "none",
                        });
                      },
                    });
                  } else if (res.cancel) {
                    uni.showToast({
                      title: "已取消",
                      icon: "none",
                    });
                  }
                },
              });
            }
          }
        })
          .catch((err) => {
            console.log(err);
          });
      });
    },
    async upVer(code) {
      let platform = uni.getSystemInfoSync().platform

      const res = await $http.base.getVersion()
      // 是否开启热更新
      if (res.data.status == 0) return
      if (res.data.vs_ban && (Number(res.data.vs_ban) > Number(code))) {
        this.downWgt(res.data.url);
      }
    },
    downWgt(wgtUrl) {
      var that = this;
      plus.nativeUI.showWaiting("正在下载更新包");
      plus.downloader.createDownload(
        wgtUrl, {
        filename: "_doc/update/",
      },
        function (d, status) {
          if (status == 200) {
            that.installWgt(d.filename); // 安装wgt包
          } else {
            plus.nativeUI.alert("安装失败" + status);
            setTimeout(() => {
              let platform = uni.getSystemInfoSync().platform
              if (platform == "android") {
                plus.runtime.openURL(encodeURI("https://api.zzlssc.cn/download"));
              } else {
                plus.runtime.openURL(encodeURI(
                  "https://apps.apple.com/cn/app/%E6%AD%A3%E8%BD%AC%E4%BB%93/id6444714389"
                ));
              }
            }, 1000);
          }
          plus.nativeUI.closeWaiting();
        }
      ).start();
    },
    installWgt(path) {
      plus.nativeUI.showWaiting("正在安装中");
      plus.runtime.install(
        path, {},
        function () {
          plus.nativeUI.closeWaiting();
          plus.nativeUI.alert(
            "更新完成",
            function () {
              plus.runtime.restart();
            },
            "",
            "确定"
          );
        },
        function (e) {
          console.log(e);
          plus.nativeUI.closeWaiting();
          plus.nativeUI.alert("更新失败请稍后尝试");
          let platform = uni.getSystemInfoSync().platform
          if (platform == "android") {
            plus.runtime.openURL(encodeURI("https://api.zzlssc.cn/download"));
          } else {
            plus.runtime.openURL(encodeURI(
              "https://apps.apple.com/cn/app/%E6%AD%A3%E8%BD%AC%E4%BB%93/id6444714389"));
          }
        }
      );
    },
  }
}
</script>

<style lang="less">
@import "./css/common.less";
::v-deep.u-swiper-indicator {
  padding-bottom: 90rpx !important;
}
::v-deep.u-indicator-item-rect {
  border-radius: 4rpx 4rpx 4rpx 4rpx !important;
}
::v-deep.uni-textarea-wrapper {
  min-height: 46rpx !important;
}
page {
  background: #f6f6f6;
}

.exceedingEllipsis {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

view,
text {
  margin: 0;
  padding: 0;
  font-family: PingFang SC-Medium, PingFang SC;
}

.nav {
  width: 750rpx;
  height: 88rpx;
  position: relative;
  font-size: 34rpx;
  font-family: PingFang SC-Bold, PingFang SC;
  font-weight: bold;
  color: #ffffff;
  z-index: 99;

  &-icon {
    position: absolute;
    width: 48rpx;
    height: 48rpx;
    left: 32rpx;
    top: 20rpx;
  }
}
.searchBox {
  width: 100%;
  height: 88rpx;
  padding: 0 32rpx;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  background-color: #fff;

  .navLeftIcon {
    width: 48rpx;
    height: 48rpx;
    margin-right: 16rpx;
  }

  .u-search {
    .u-content {
      height: 66rpx !important;
    }
  }

  .u-icon__img {
    width: 40rpx !important;
    height: 40rpx !important;
  }
}

.flex-center-center {
  display: flex;
  align-items: center;
  justify-content: center;
}

.flex-center-between {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.nodata-st {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 40rpx;
  margin-top: 20rpx;
  margin-bottom: 40rpx;
  width: 100%;
  color: #999999;
}
</style>
<style lang="scss">
@import "@/uni_modules/vk-uview-ui/theme.scss";
@import "./uni_modules/vk-uview-ui/index.scss";
</style>